package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.pojo.dto.AlbumUpdateDTO;
import cn.tedu.csmall.product.pojo.dto.BrandAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.BrandUpdateDTO;
import cn.tedu.csmall.product.pojo.dto.CategoryUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.service.IBrandService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "04品牌管理模版")
@Slf4j
@Validated
@RestController
@RequestMapping("/brands")
public class BrandController {
    @Autowired
    private IBrandService iBrandService;

    public BrandController(){
        log.debug("创建控制器对象：BrandController");
    }
    // http://localhost:8080/add-new-brand?name=卡修斯
    @ApiOperation("添加品牌")
    @PostMapping("/add-new")
    public String addNew(BrandAddNewDTO brandAddNewDTO){
        log.debug("开始处理品牌请求,参数:{}",brandAddNewDTO);
            iBrandService.addNew(brandAddNewDTO);
            log.debug("添加品牌请求成功");
            return "添加品牌请求成功";


    }

    @ApiOperation("根据id删除品牌")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id",value = "品牌id",required = true,dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@Range(min = 1,message = "删除品牌失败,尝试删除相册的ID无效")
                             @PathVariable Long id){
        log.debug("开始处理品牌请求,参数:{}",id);
        iBrandService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("查询品牌列表")
    @ApiOperationSupport(order = 300)
    @GetMapping("")
    public JsonResult<List<BrandListItemVO>> list(){
        log.debug("开始处理查询品牌列表请求");
        List<BrandListItemVO> list = iBrandService.list();
        return JsonResult.ok(list);
    }

    @ApiOperation("启用品牌")
    @ApiOperationSupport(order = 410)
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> Enable(@Range(min = 1,message = "删除品牌失败,尝试删除相册的ID无效")
                                   @PathVariable Long id){
        log.debug("开始处理启用品牌服务");
        iBrandService.setEnable(id);
        return JsonResult.ok();
    }

    @ApiOperation("禁用品牌")
    @ApiOperationSupport(order = 420)
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> Disable(@Range(min = 1,message = "删除品牌失败,尝试删除相册的ID无效")
                                   @PathVariable Long id){
        log.debug("开始处理启用品牌服务");
        iBrandService.setDisable(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据id查询相册详情")
    @ApiOperationSupport(order = 310)
    @GetMapping("/{id:[0-9]+}/select")
    public JsonResult<BrandStandardVO> selectById(@PathVariable Long id){
        BrandStandardVO brandStandardVO = iBrandService.selectById(id);
        return JsonResult.ok(brandStandardVO);
    }

    @ApiOperation("修改详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "相册id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, BrandUpdateDTO brandUpdateDTO) {
        log.debug("开始处理【修改相册详情】的请求，参数ID：{}, 新数据：{}", id, brandUpdateDTO);
        iBrandService.update(id,brandUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("重建品牌缓存")
    @ApiOperationSupport(order = 600)
    @PostMapping("/cache/rebuild")
    public JsonResult<Void> rebuildCache(){
        log.debug("开始处理重建品牌缓存请求");
        iBrandService.rebuildCache();
        return JsonResult.ok();
    }

}
